#include "item.h"

Item::Item()
{
    switch (iType)
    {
        case 0:
            MyBackgroundColor = Qt::yellow;
        break;
        case 1:
            MyBackgroundColor = Qt::lightGray;
        break;
        case 2:
            MyBackgroundColor = Qt::green;
        break;
        case 3:
            MyBackgroundColor = Qt::white;
        break;
    }
    setFlags(ItemIsMovable | ItemIsSelectable | ItemSendsScenePositionChanges);
}

Item::Item(int iType)
{
    iID = id++;
    iChildId = -1;
    ParentItem = NULL;
    iParentItemId = -1;
    this->iType = iType;
    switch (iType)
    {
        case 0:
            MyBackgroundColor = Qt::yellow;
        break;
        case 1:
            MyBackgroundColor = Qt::lightGray;
        break;
        case 2:
            MyBackgroundColor = Qt::green;
        break;
        case 3:
            MyBackgroundColor = Qt::white;
        break;
    }

    setFlags(ItemIsMovable | ItemIsSelectable | ItemSendsScenePositionChanges);
}

Item::~Item()
{
    while (MyLine.count())
    {
        Line *l = MyLine.first();
        delete l;
    }
    MyLine.clear();
    while (ChildItem.count())
    {
        Item * c = ChildItem.first();
        c->SetParent(NULL);
		ChildItem.removeAt(0);
        delete c;
    }
    if (ParentItem != NULL)
    {
        ParentItem->RemoveChild(this);
        ParentItem = NULL;
    }
}

void Item::RemoveLink(Line *line)
{
    MyLine.removeOne(line);
}

void Item::AddLine(Line *line)
{
    MyLine << line;
}

void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QPen pen;
    if (option->state & QStyle::State_Selected)
        pen.setWidth(3);

    painter->setPen(pen);
    painter->setBrush(MyBackgroundColor);

    QRectF rect = outlineRect();
    if (iType != 3)
        painter->drawRoundRect(rect, 1200 / rect.width(), 1200 / rect.height());
    painter->drawText(rect, Qt::AlignTop | Qt::AlignHCenter, sText);
    for (int i = 0; i < ChildItem.count(); i++)
        if (ChildItem[i] != NULL)
        {
            if (i == 3)
                i = 3;
            ChildItem[i]->setX(pos().x() - boundingRect().width() / 2 + ChildItem[i]->boundingRect().width() / 2 + GetRectsWidth(ChildItem, i) + i * 3);
            ChildItem[i]->setY(pos().y() + 5);
        }
}

float Item::GetRectsWidth(QList<Item *> list, int iCount) const
{
    float fResult = 0;
    for (int i = 0; i < iCount; i++)
        fResult += list[i]->outlineRect().width();
    return fResult;
}

QRectF Item::boundingRect() const
{
    const int Margin = 1;
    return outlineRect().adjusted(-Margin, -Margin, Margin, Margin);
}

QRectF Item::outlineRect() const
{
    const int iPadding = 8;
    QFontMetricsF m = qApp->font();
    QRectF rect = m.boundingRect(sText);
    if (ChildItem.count())
    {
        float x = 0, y = 0;
        for (int i = 0; i < ChildItem.count(); i++)
        {
                x += ChildItem[i]->outlineRect().width();
                if (i > 2)
                    y += qMax(ChildItem[i - 2]->outlineRect().height(), qMax(ChildItem[i]->outlineRect().height(), ChildItem[i - 1]->outlineRect().height()));
        }
        rect.setWidth(x);
        float dY = 0;
        if (ChildItem.count() > 1)
            dY = qMax(ChildItem[0]->outlineRect().height(), ChildItem[1]->outlineRect().height());
        else
            if (ChildItem.count() == 1)
                dY = ChildItem[0]->outlineRect().height();
            else
                dY = qMax(ChildItem[1]->outlineRect().height(), qMax(ChildItem[2]->outlineRect().height(), ChildItem[0]->outlineRect().height()));
        rect.setHeight(y + dY);
    }
    rect.adjust(-iPadding, -iPadding, iPadding, iPadding);
    rect.translate(-rect.center());
    return rect;
}

int Item::GetRoundness(double Size) const
{
    const int iDiameter = 8;
    return 100 * iDiameter / Size;
}

void Item::SetText(QString text, bool lType)
{
	if (!lType)
		sText = text + " " + QString::number(iID);
	else
		sText = text;
    prepareGeometryChange();
    update();
}

void Item::SetChild(Item *child)
{
    ChildItem << child;
    setZValue(-1);
    prepareGeometryChange();
    update();
}

void Item::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    OpenProperties();
}

void Item::OpenProperties()
{
    DlgProperties dlg(this);
    dlg.exec();
    this->scene()->update();
}

QList<Item *> Item::GetChild() const
{
    return ChildItem;
}

int Item::GetType() const
{
    return iType;
}

int Item::GetChildId() const
{
    if (!ChildItemId.count())
        return -1;
    else
        return ChildItemId.first();
}

int Item::GetId() const
{
    return iID;
}

void Item::SetId(int id)
{
    iID = id;
}

void Item::SetChildId(int id)
{
    iChildId = id;
}

void Item::SetChildIds(QList<int> id)
{
    ChildItemId.append(id);
}

QString Item::GetText() const
{
    return sText;
}

void Item::SetParent(Item *parent)
{
    ParentItem = parent;
    if (ParentItem != NULL)
        iParentItemId = parent->GetId();
}

void Item::SetParentId(int iParentId)
{
    iParentItemId = iParentId;
}

Item * Item::GetParent() const
{
    return ParentItem;
}

int Item::GetParentId() const
{
    return this->iParentItemId;
}

void Item::RemoveChild(Item *child)
{
    ChildItem.removeOne(child);
}

int Item::GetChildCount() const
{
    return ChildItemId.count();
}

QList<int> Item::GetChildIds() const
{
    QList<int> result;
    for (int i = 0; i < ChildItem.count(); i++)
        result << ChildItem[i]->GetId();
    return result;
}

int Item::IsChild(int id) const
{
    return ChildItemId.indexOf(id);
}

Item * Item::GetChildAt(int iPos)
{
    return ChildItem[iPos];
}

int Item::GetChildIdAt(int iPos)
{
    return ChildItemId[iPos];
}